<?php
import( 'de.ceus-media.framework.krypton.core.Logic' );
import( 'classes.model.Topic' );
import( 'classes.model.Project' );
//import( 'de.ceus-media.framework.krypton.core.database.StatementBuilder' );
/**
 *	Logic of Licencee, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
/**
 *	Logic of Licencee, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@author			David Seebacher <dseebacher@gmail.com>
 *	@since			13.03.2007
 *	@version		0.1
 */
class Logic_Topic extends Framework_Krypton_Core_Logic
{
	function addTopic( $data, $prefix = "" )
	{
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$this->validateForm( 'management/structure', 'addTopic', $data, $prefix );
		$model	= new Model_Project( $data['project_id'] );
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_project_id' );
		$model	= new Model_Topic( $data['parent_id'] );
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_parent_id' );
		$data['created']	= time();
		return $model->insert( $data );
	}
	
	function editTopic( $topicId, $data, $prefix = "" )
	{
		if( !$topicId )
		 	throw new Framework_Krypton_Exception_Logic( 'topic_id_missing' );
		$model	= new Model_Topic( $topicId );
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'invalid_topic_id' );
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$this->validateForm( 'management/structure', 'editTopic', $data, $prefix );
		$result	= $model->update( $data );
		if( $result )
			$model->update( array( 'modified' => time() ) );
		return $result;
	}
	
	static function getTopicList( $projectId, $parentId = 0, $level = 0, $levelIndicator = "-", $factor = 1 )
	{
		$list	= array();
		$model	 = new Model_Topic();
		$model->focusIndex( 'project_id', $projectId );
		$model->focusIndex( 'parent_id', $parentId );
		$topics	= $model->get( false );
		foreach( $topics as $topic )
		{
			$indent	= str_repeat( $levelIndicator, $level * $factor );
			$list[$topic['topic_id']] = $indent.$topic['title'];
			$sublist	= self::getTopicList( $projectId, $topic['topic_id'], $level + 1, $levelIndicator, $factor );
			foreach( $sublist as $id => $title )
				$list[$id] = $title;
		}
		return $list;
	}
	
	static function getTopicData( $topicId )
	{
		$model	= new Model_Topic( $topicId );
		$data	= $model->get();
		if( !$data )
			throw new Framework_Krypton_Exception_Logic( 'invalid_topic_id' );
		return $data;
	}
	
	static function getTopicDescription( $topicId )
	{
		$data	= self::getTopicData( $topicId );
		return $data['description'];
	}

	static function getTopicTitle( $topicId )
	{
		$data	= self::getTopicData( $topicId );
		return $data['title'];
	}
}
?>